package com.example.fitapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.fragment.HomeFragment;
import com.example.fitapp.activity.fragment.TabFragment;
import com.example.fitapp.activity.fragment.UsFragment;
import com.example.fitapp.activity.utils.L;
import com.example.fitapp.activity.view.TabView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivityWithTap extends AppCompatActivity{

    private ViewPager mViewPager;
    private List<String> mTitles = new ArrayList<>(Arrays.asList("首页","个人中心"));
    private TabView mButtonIndex;
    private LinearLayout mButtonCreate;
    private TabView mButtonUs;

    private List<TabView> mTabs= new ArrayList<>();

    private SparseArray<Fragment> mFragements = new SparseArray<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);

        initViews();
        initViewPagerAdapter();
        initEvents();
    }

    private void initEvents() {
        for(int i = 0;i < mTabs.size();i++){
            TabView tabView = mTabs.get(i);
            final int finalI = i;
            tabView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(finalI,false);
                    setCurrentTab(finalI);
                }
            });
        }
    }

    private void initViewPagerAdapter() {
        //        设置缓存多少个不会被销毁
        mViewPager.setOffscreenPageLimit(mTitles.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                if(position==0){
                    fragment = HomeFragment.newInstance(mTitles.get(position));
                }
                if(position==1){
                    fragment = UsFragment.newInstance(mTitles.get(position));
                }
                mFragements.put(position,fragment);
                return fragment;
            }

            @Override
            public int getCount() {
                return mTitles.size();
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                Fragment fragment = null;
                if(position==0){
                     fragment = (HomeFragment) super.instantiateItem(container, position);
                }
                if(position==1){
                     fragment = (UsFragment) super.instantiateItem(container, position);
                }
                mFragements.put(position,fragment);
                return fragment;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                mFragements.remove(position);
                super.destroyItem(container, position, object);
            }
        });
        // 监听页面滑动
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //左->右 positionOffset 0->1
                //left progress:1~0(1-positionOffset)  right progress:0~1(positionOffset)
                //右->左 positionOffset 1->0
                //left progress:0~1(1-positionOffset)  right progress:1~0(positionOffset)

                if(positionOffset > 0){
                    TabView left = mTabs.get(position);
                    TabView right = mTabs.get(position+1);
                    left.setProgress((1-positionOffset));
                    right.setProgress(positionOffset);
                }

            }
            //监听切换完成的事件
            @Override
            public void onPageSelected(int position) {
                L.d("onPageSelected pos ="+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initViews(){
        mViewPager = findViewById(R.id.vp_main);
        mButtonIndex = findViewById(R.id.tap_index);
        mButtonCreate =  findViewById(R.id.tap_cerate);
        mButtonUs = findViewById(R.id.tap_us);

        mButtonIndex.setIconAndText(R.drawable.index,R.drawable.index_activity,"社区");
        mButtonUs.setIconAndText(R.drawable.us,R.drawable.us_activity,"我的");
        mButtonCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.d("打开发布页面");
                Intent intent = new Intent(MainActivityWithTap.this,PushActivity.class);
                startActivity(intent);
            }
        });
        mTabs.add(mButtonIndex);
        mTabs.add(mButtonUs);

        setCurrentTab(0);

    }

    private void setCurrentTab(int pos){
        for(int i = 0;i < mTabs.size();i++){
            TabView tabView = mTabs.get(i);
            if(i == pos){
                tabView.setProgress(1);
            }else{
                tabView.setProgress(0);
            }
        }
    }


}